<?php
function Priv(&$socket, $data, $public_data, $priv_modules){
	//stream_set_blocking($socket,true);
	echo date("[H:i:s] d-m-Y").": ".$data;
		
	flush();
	$ex = explode(' ', $data);
	$rawcmd = str_replace(chr(13), '', str_replace(chr(10), '', explode(':', $ex[3]))); //removes newlines and get the command
	//$oneword = explode('<br>', $rawcmd);
	$channel = $ex[2];
	
	$nicka = explode('@', $ex[0]);
	$nickb = explode('!', $nicka[0]);
	$nickc = explode(':', $nickb[0]);

	$host = $nicka[1];
	$nick = $nickc[1];
	echo $host."\n";
    
	//we gotta respond to the PING's with a PONG
	if($ex[0] == "PING"){
		fputs($socket, "PONG ".$ex[1]."\n");
	}else{
		$args = ""; 
		for ($i = 4; $i < count($ex); $i++) { 
			$args .= $ex[$i].' '; 
		}
		
		$public_data = array($nick, $channel, $rawcmd[1], $rawcmd[2], $args, $users, false);	
		echo "COMMAND:".$ex[1]."\n";
		//time to check what Jacqli will do
		if($ex[1] == "PRIVMSG"){
			for($i = 0; $i < sizeof($priv_modules); $i++){
				for($j = 0; $j < sizeof($priv_modules[$i]); $j++){
					$function = str_replace('.','',getFileName($priv_modules[$i][$j]));
					echo "$function\n\n";
					$public_data = $function($public_data);
					$public_data[6] = html_entity_decode($public_data[6],ENT_QUOTES,"UTF-8");
					if($public_data[6] != false){
						$currmsg = msg($socket, "PRIVMSG ".$channel." : $public_data[6]\n",$currmsg);
						$i = 500; $j = 500;
					}
				}
			}
		}else{
			
		}
	}
	return $public_data;
}

function msg(&$sock, $arg, $currmsg){
	fputs($sock, $arg);
	return ($currmsg+5);
}
?>